package com.yugao.fintech.draper.datascope.strategy;

import com.yugao.fintech.draper.auth.api.AuthFeignApi;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.security.SecurityUser;
import com.yugao.fintech.draper.core.exception.BaseException;
import com.yugao.fintech.draper.core.model.DeptDataScopeDTO;
import com.yugao.fintech.draper.core.util.ResultConsumer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 基于部门的数据权限 Framework Service
 */
@Slf4j
@RequiredArgsConstructor
public class DepartmentDataScopeService {

	private final AuthFeignApi roleFeignApi;

	/**
	 * 获得登陆用户的部门数据权限
	 * @param loginUser 登陆用户
	 * @return 部门数据权限
	 */
	public DeptDataScopeDTO getDeptDataPermission(SecurityUser loginUser) {
		ResultVO<DeptDataScopeDTO> resultVO = roleFeignApi.getDataPermission();
		// @formatter:off
		return ResultConsumer.ofNullable(roleFeignApi.getDataPermission())
				.assertSuccess(r -> new BaseException(String.format("查询数据权限接口失败:%s", loginUser.getUsername())))
				.peek(d -> log.info("数据权限是:{}", d)).getData();
		// @formatter:on
	}
}
